<template>
    <div class="main">
        <div class="workspace">
            <form class="login-form" @submit.prevent="handleLogin">
                <h2>学生组织登录</h2>
                <div class="form-account">
                    <label for="username">账号：</label>
                    <input type="text" id="username" v-model="inputname" placeholder="请输入账号" required
                        @input="clearError">
                </div>
                <div class="password-form">
                    <label for="password">密码：</label>
                    <input type="password" id="password" v-model="inputpassword" placeholder="请输入密码" required
                        @input="clearError" @keyup.enter="handleLogin">
                </div>
                <div v-if="errorMessage" class="error-message">
                    {{ errorMessage }}
                </div>
                <button type="submit" class="form-button">
                    登录
                </button>
            </form>
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router';
import axios from 'axios';
const router = useRouter();
const inputname = ref('')
const inputpassword = ref('')
const errorMessage = ref('')

const clearError = () => {
    errorMessage.value = '';
}

// const handleLogin = () => {
//     // 假设本地账号和密码都是org
//     if (inputname.value === 'org' && inputpassword.value === 'org') {
//         localStorage.setItem('loginId', inputname.value); // 保存登录id到本地
//         ElMessage.success('登录成功');
//         router.push('/somain');
//     } else {
//         ElMessage.error('用户名或密码错误');
//     }
// }

const handleLogin = async () => {
    try {
        const response = await axios.post('http://localhost:8080/orgs/login', {
            "loginId": inputname.value,
            "password": inputpassword.value,
        });
        if (response.data.code === 200) {
            ElMessage.success('登录成功');
            localStorage.setItem('loginId', inputname.value); // 保存登录id到本地
            router.push('/somain');
        } else {
            ElMessage.error(response.data.msg || '用户名或密码错误');
        }
    } catch (error) {
        ElMessage.error('网络错误');
    }
}
</script>



<style lang="scss" scoped>
.main {
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
}

.workspace {
  display: flex;
  height: 100%;
  width: 420px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 18px;
  box-shadow: 0 6px 32px rgba(90, 169, 233, 0.15);
  align-items: center;
  justify-content: center;
}

.login-form {
  width: 100%;
  padding: 2.5rem 2rem 2rem 2rem;
  display: flex;
  flex-direction: column;
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 2px 12px rgba(90, 169, 233, 0.10);

  h2 {
    text-align: center;
    color: #357ab8;
    font-size: 2rem;
    font-weight: bold;
    margin-bottom: 2rem;
    letter-spacing: 2px;
  }

  .form-account,
  .password-form {
    margin-bottom: 1.5rem;

    label {
      display: block;
      margin-bottom: 0.5rem;
      color: #357ab8;
      font-size: 1.1rem;
      font-weight: 500;
    }

    input {
      width: calc(100% - 30px); // 让右边留出空间
      padding: 0.9rem;
      padding-right: 12px; // 右侧内边距
      border: 1.5px solid #bcdffb;
      border-radius: 6px;
      font-size: 1.1rem;
      background: #f7fbfe;
      transition: border 0.2s;

      &:focus {
        border-color: #5ba9e9;
        outline: none;
        background: #fff;
      }
    }
  }

  .error-message {
    color: #f56c6c;
    margin-bottom: 1.2rem;
    text-align: center;
    font-size: 1rem;
    letter-spacing: 1px;
  }

  .form-button {
    width: 100%;
    padding: 0.9rem;
    background: linear-gradient(90deg, #5ba9e9 0%, #357ab8 100%);
    color: #fff;
    border: none;
    border-radius: 6px;
    font-size: 1.15rem;
    font-weight: bold;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(90, 169, 233, 0.10);
    transition: background 0.2s, box-shadow 0.2s;

    &:hover:not(:disabled) {
      background: linear-gradient(90deg, #357ab8 0%, #5ba9e9 100%);
      box-shadow: 0 4px 16px rgba(90, 169, 233, 0.18);
    }
  }
}
</style>